package function;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import common.ListOperate;
import common.PinyinUtils;
import common.PostJson;
import form.PassengerForm;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;

public class IntercityAction {
	private static String GET_ALL_LINES_URL = "http://106.15.43.138:8080/api/line/all_lines";
	private static String GET_LOCATION_INFO = "http://106.15.43.138:8080/api/user/get_location_info";
	private static String GET_HISTORY_ADDRESS = "http://106.15.43.138:8080/api/order/address_list";
	private static String GET_START_LINES = "http://106.15.43.138:8080/api/svconfig/get_start_lines";
	private static String GET_END_LINES = "http://106.15.43.138:8080/api/svconfig/get_end_lines";
	private static String GET_LINE_PRICE = "http://106.15.43.138:8080/api/price/v";
	private static String GET_COUNT_PRICE = "http://106.15.43.138:8080/api/price/get_metered_fee";
	private static String GET_DRIVER_LOCATION = "http://106.15.43.138:8080/bak/driver_dispatched/location";
	private static String GET_AVAILABLE_CAR = "http://106.15.43.138:8080/api/price/car_type_available";
	private static String GET_ORDERS_ALL = "http://106.15.43.138:8080/api/order/lists_new";

	// 获取所有线路√
	public static String getAllLines() {
		JSONObject json = new JSONObject();
		json.put("version", "0");
		String aJson = PostJson.postJson(json, GET_ALL_LINES_URL);
		return aJson;
	}

	// 城市已开通服务√
	public static String cityService(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_LOCATION_INFO);
		JSONObject bJson = JSONObject.fromObject(aJson);
		String cJson = bJson.getString("client_menu");
		return cJson;
	}

	// 获取历史地址√
	public static String historyAddress(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_HISTORY_ADDRESS);
		return aJson;
	}

	// 获取起点线路√
	public static String startLines() throws BadHanyuPinyinOutputFormatCombination {
		JSONObject json = new JSONObject();
		json.put("version", 0);
		json.put("app", "0");
		String aJson = PostJson.post(json.toString(), GET_START_LINES);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if (!bJson.getString("status").equals("0")) {
			return aJson;
		}
		JSONArray jStartCities = bJson.getJSONArray("start_cities");
		String sortCity = "[{\"alphabet\":\"A\"},{\"alphabet\":\"B\"},{\"alphabet\":\"C\"},{\"alphabet\":\"D\"},{\"alphabet\":\"E\"},{\"alphabet\":\"F\"},{\"alphabet\":\"G\"},{\"alphabet\":\"H\"},{\"alphabet\":\"I\"},{\"alphabet\":\"J\"},{\"alphabet\":\"K\"},{\"alphabet\":\"L\"},{\"alphabet\":\"M\"},{\"alphabet\":\"N\"},{\"alphabet\":\"O\"},{\"alphabet\":\"P\"},{\"alphabet\":\"Q\"},{\"alphabet\":\"R\"},{\"alphabet\":\"S\"},{\"alphabet\":\"T\"},{\"alphabet\":\"U\"},{\"alphabet\":\"V\"},{\"alphabet\":\"W\"},{\"alphabet\":\"X\"},{\"alphabet\":\"Y\"},{\"alphabet\":\"Z\"}]";
		JSONArray jaSortCity = JSONArray.fromObject(sortCity);
		for (int k = 0; k < jaSortCity.size(); k++) {
			JSONObject jAlphabet = jaSortCity.getJSONObject(k);
			JSONArray cities = new JSONArray();
			jAlphabet.put("cities", cities);
		}
		for (int j = 0; j < jStartCities.size(); j++) {
			JSONObject jName = jStartCities.getJSONObject(j);
			String name = jName.getString("name");
			String firstLetters = PinyinUtils.chineseToPinYinShort(name);
			String allLetters = PinyinUtils.chineseToPinYinFull(name);
			String alphabet = PinyinUtils.chineseToPinYinFirst(name).toUpperCase();
			jName.put("alphabet", alphabet);
			jName.put("alphabet_first_letters", firstLetters);
			jName.put("alphabet_all", allLetters);
			for (int i = 0; i < jaSortCity.size(); i++) {
				JSONObject jAlphabet = jaSortCity.getJSONObject(i);
				JSONArray cities = jAlphabet.getJSONArray("cities");
				String ch = jAlphabet.getString("alphabet");
				if (ch.equals(alphabet)) {
					cities.add(jName);
				}
			}
		}
		return jaSortCity.toString();
	}

	// 获取终点线路√
	public static String endLines(JSONObject json) throws BadHanyuPinyinOutputFormatCombination {
		json.put("version", 0);
		json.put("app", "0");
		String aJson = PostJson.post(json.toString(), GET_END_LINES);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if (!bJson.getString("status").equals("0")) {
			return aJson;
		}
		JSONArray jEndCities = bJson.getJSONArray("to_cities");
		String sortCity = "[{\"alphabet\":\"A\"},{\"alphabet\":\"B\"},{\"alphabet\":\"C\"},{\"alphabet\":\"D\"},{\"alphabet\":\"E\"},{\"alphabet\":\"F\"},{\"alphabet\":\"G\"},{\"alphabet\":\"H\"},{\"alphabet\":\"I\"},{\"alphabet\":\"J\"},{\"alphabet\":\"K\"},{\"alphabet\":\"L\"},{\"alphabet\":\"M\"},{\"alphabet\":\"N\"},{\"alphabet\":\"O\"},{\"alphabet\":\"P\"},{\"alphabet\":\"Q\"},{\"alphabet\":\"R\"},{\"alphabet\":\"S\"},{\"alphabet\":\"T\"},{\"alphabet\":\"U\"},{\"alphabet\":\"V\"},{\"alphabet\":\"W\"},{\"alphabet\":\"X\"},{\"alphabet\":\"Y\"},{\"alphabet\":\"Z\"}]";
		JSONArray jaSortCity = JSONArray.fromObject(sortCity);
		for (int k = 0; k < jaSortCity.size(); k++) {
			JSONObject jAlphabet = jaSortCity.getJSONObject(k);
			JSONArray cities = new JSONArray();
			jAlphabet.put("cities", cities);
		}
		for (int j = 0; j < jEndCities.size(); j++) {
			JSONObject jName = jEndCities.getJSONObject(j);
			String name = jName.getString("name");
			String firstLetters = PinyinUtils.chineseToPinYinShort(name);
			String allLetters = PinyinUtils.chineseToPinYinFull(name);
			String alphabet = PinyinUtils.chineseToPinYinFirst(name).toUpperCase();
			jName.put("alphabet", alphabet);
			jName.put("alphabet_first_letters", firstLetters);
			jName.put("alphabet_all", allLetters);
			for (int i = 0; i < jaSortCity.size(); i++) {
				JSONObject jAlphabet = jaSortCity.getJSONObject(i);
				JSONArray cities = jAlphabet.getJSONArray("cities");
				String ch = jAlphabet.getString("alphabet");
				if (ch.equals(alphabet)) {
					cities.add(jName);
				}
			}
		}
		return jaSortCity.toString();
	}

	// 获取地址码
	public static String lotCode(JSONArray jCity) {
		int flag = 0;
		JSONObject json = new JSONObject();
		json.put("version", 0);
		json.put("app", "0");
		String aJson = PostJson.post(json.toString(), GET_START_LINES);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if (!bJson.getString("status").equals("0")) {
			return aJson;
		}
		JSONArray jStartCities = bJson.getJSONArray("start_cities");
		JSONObject joLotCode = new JSONObject();
		JSONArray jaLotcode = new JSONArray();
		for (int k = 0; k < jCity.size(); k++) {
			JSONObject cityName = jCity.getJSONObject(k);
			for (int i = 0; i < jStartCities.size(); i++) {
				JSONObject jChild = jStartCities.getJSONObject(i);
				JSONArray jChildCities = jChild.getJSONArray("child_citys");
				for (int j = 0; j < jChildCities.size(); j++) {
					JSONObject jCities = jChildCities.getJSONObject(j);
					if ((cityName.getString("name")).equals(jCities.getString("name"))) {
						flag = 1;
						joLotCode.put("message", "success");
						joLotCode.put("name", jCities.getString("name"));
						joLotCode.put("lot_code", jCities.getString("code"));
					}
				}
			}
			jaLotcode.add(joLotCode);
		}
		if (flag == 0) {
			joLotCode.put("message", "失败");
			return joLotCode.toString();
		}
		return jaLotcode.toString();
	}

	// 线路价格1
	public static String linePrice(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_LINE_PRICE);
		return aJson;
	}

	// 线路价格2
	public static String countPrice(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_COUNT_PRICE);
		return aJson;
	}

	// 实时获取司机位置
	public static String driverLocation(JSONObject json) {
		String aJson = PostJson.post(json.toString(), GET_DRIVER_LOCATION);
		return aJson;
	}

	// 获取可用车型
	public static String availableCar(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_AVAILABLE_CAR);
		return aJson;
	}

	// 根据出发地获取最多/最新目的地址
	public static String getAddress(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_ORDERS_ALL);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if (!bJson.getString("status").equals("0")) {
			return aJson;
		}
		JSONArray jaLists = bJson.getJSONArray("list");
		String Code = json.getString("lot_code");
		JSONArray addList = new JSONArray();
		// 根据出发地的code，获取所有的目的地
		for (int i = 0; i < jaLists.size(); i++) {
			JSONObject joList = jaLists.getJSONObject(i);
			String lineId = joList.getString("line_id");
			if (Code.equals(lineId.substring(0, 6))) {
				int index = lineId.lastIndexOf("_");
				int lastIndex = lineId.length();
				String endLotCode = lineId.substring(index + 1, lastIndex);
				JSONObject locations = joList.getJSONObject("locations");
				JSONObject start = locations.getJSONObject("start");
				JSONObject end = locations.getJSONObject("end");
				JSONObject lotInfo = new JSONObject();
				lotInfo.put("start_address", start.getString("address"));
				lotInfo.put("start_location", start.getString("location"));
				lotInfo.put("end_code", endLotCode);
				lotInfo.put("end_address", end.getString("address"));
				lotInfo.put("end_location", end.getString("location"));
				addList.add(lotInfo);
			}
		}
		if (addList.size() == 0) {
			JSONObject message = new JSONObject();
			message.put("message", "未找到目的地");
			return message.toString();
		}
		List<String> endAdd = new ArrayList<>();
		List<String> newEndAdd = new ArrayList<>();
		// 定义endAdd的list
		JSONObject endAddress = new JSONObject();
		JSONObject latest = new JSONObject();
		latest.put("lot_code", addList.getJSONObject(0).getString("end_code"));
		latest.put("address", addList.getJSONObject(0).getString("end_address"));
		latest.put("location", addList.getJSONObject(0).getString("end_location"));
		endAddress.put("latest", latest);
		for (int i = 0; i < addList.size(); i++) {
			endAdd.add(addList.getJSONObject(i).getString("end_address"));
		}
		newEndAdd = endAdd;
		int[] count = new int[newEndAdd.size()];
		// 计算各个元素数量
		for (int i = 0; i < newEndAdd.size(); i++)
			for (int j = 0; j < endAdd.size(); j++) {
				if (newEndAdd.get(i).equals(endAdd.get(j))) {
					count[i]++;
				}
			}
		int max = 0;
		int index = 0;
		for (int i = 0; i < newEndAdd.size(); i++) {
			if (count[i] > max) {
				max = count[i];
				index = i;
			}
		}
		JSONObject most = new JSONObject();
		most.put("lot_code", addList.getJSONObject(index).getString("end_code"));
		most.put("address", addList.getJSONObject(index).getString("end_address"));
		most.put("location", addList.getJSONObject(index).getString("end_location"));
		endAddress.put("most", most);
		JSONArray other = new JSONArray();
		for (int i = 0; i < newEndAdd.size(); i++) {
			if (i != 0 && i != index) {
				JSONObject otherObj = new JSONObject();
				otherObj.put("lot_code", addList.getJSONObject(i).getString("end_code"));
				otherObj.put("address", addList.getJSONObject(i).getString("end_address"));
				otherObj.put("location", addList.getJSONObject(i).getString("end_location"));
				if (!other.contains(otherObj) && !most.equals(otherObj) && !latest.equals(otherObj)) {
					other.add(otherObj);
				}

			}
		}
		endAddress.put("other", other);
		return endAddress.toString();
	}

	// 根据当前用户获取历史乘车人的信息，并按照姓氏排序
	public static String historyPassengers(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_ORDERS_ALL);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if (!bJson.getString("status").equals("0")) {
			return aJson;
		}
		JSONArray jaLists = bJson.getJSONArray("list");
		JSONArray passList = new JSONArray();
		// 获取所有乘客信息，存入passList中
		for (int i = 0; i < jaLists.size(); i++) {
			JSONObject joList = jaLists.getJSONObject(i);
			JSONObject locations = joList.getJSONObject("locations");
			JSONObject start = locations.getJSONObject("start");
			JSONObject passInfo = new JSONObject();
			passInfo.put("name", start.getString("name"));
			passInfo.put("phone", start.getString("phone"));
			passList.add(passInfo);
		}
		@SuppressWarnings("unchecked")
		List<PassengerForm> passForm = (List<PassengerForm>) JSONArray.toCollection(passList, PassengerForm.class);
		List<PassengerForm> newPassForm = ListOperate.removeDuplicate(passForm);
		Collections.sort(newPassForm, new Comparator<PassengerForm>() {

			@Override
			public int compare(PassengerForm o1, PassengerForm o2) {
				return o1.getName().compareTo(o2.getName());
			}

		});
		JSONArray passengers = JSONArray.fromObject(newPassForm);
		return passengers.toString();
	}

	// 根据地址编码，获取目的地名称，坐标
	public static String destinyInfo(JSONObject json) {
		String aJson = PostJson.postJson(json, GET_ORDERS_ALL);
		JSONObject bJson = JSONObject.fromObject(aJson);
		if (!bJson.getString("status").equals("0")) {
			return aJson;
		}
		JSONArray jaLists = bJson.getJSONArray("list");
		String Code = json.getString("lot_code");
		JSONArray desList = new JSONArray();
		// 根据出发地的code，获取所有的目的地
		for (int i = 0; i < jaLists.size(); i++) {
			JSONObject joList = jaLists.getJSONObject(i);
			String lineId = joList.getString("line_id");
			if (Code.equals(lineId.substring(0, 6))) {
				JSONObject locations = joList.getJSONObject("locations");
				JSONObject end = locations.getJSONObject("end");
				JSONObject lotInfo = new JSONObject();
				lotInfo.put("end_address", end.getString("address"));
				lotInfo.put("end_location", end.getString("location"));
				desList.add(lotInfo);
			}
		}
		return desList.toString();
	}

	// 根据搜索条件，获取地址
	public static String searchAdd(JSONObject json) throws BadHanyuPinyinOutputFormatCombination {
		JSONObject jo = new JSONObject();
		jo.put("version", 0);
		jo.put("app", "0");
		String aJson;
		JSONObject bJson = new JSONObject();
		JSONArray jStartCities = new JSONArray();
		if (!json.has("type")) {
			JSONObject message = new JSONObject();
			message.put("message", "缺少type,1为获取目的地,0为获取出发地！");
			return message.toString();
		}
		if (json.get("type").equals(0)) {
			aJson = PostJson.post(jo.toString(), GET_START_LINES);
			bJson = JSONObject.fromObject(aJson);
			jStartCities = bJson.getJSONArray("start_cities");
		} else if (json.get("type").equals(1)) {
			if (!json.has("code")) {
				JSONObject message = new JSONObject();
				message.put("message", "缺少code");
				return message.toString();
			}
			jo.put("code", json.getString("code"));
			aJson = PostJson.post(jo.toString(), GET_END_LINES);
			bJson = JSONObject.fromObject(aJson);
			jStartCities = bJson.getJSONArray("to_cities");
		} else {
			JSONObject message = new JSONObject();
			message.put("message", "参数错误！");
			return message.toString();
		}
		String search = json.getString("search");
		JSONArray getAdd = new JSONArray();
		for (int i = 0; i < jStartCities.size(); i++) {
			JSONObject jChild = jStartCities.getJSONObject(i);
			String searchNames = PinyinUtils.chineseToPinYinShort(jChild.getString("name"));
			String searchNamesAll = PinyinUtils.chineseToPinYinFull(jChild.getString("name"));
			String searchNamesFirst = PinyinUtils.chineseToPinYinFirst((jChild.getString("name")));
			if (PinyinUtils.isChinese(search)) {
				if (jChild.getString("name").contains(search)) {
					getAdd.add(jChild);
				}
			} else {
				String aSearch = PinyinUtils.chineseToPinYinFull(search).toLowerCase();
				if (aSearch.equals(searchNamesFirst)) {
					getAdd.add(jChild);
				} else if (searchNames.contains(aSearch)) {
					getAdd.add(jChild);
				} else if (searchNamesAll.contains(aSearch)) {
					getAdd.add(jChild);
				}
			}
		}
		return getAdd.toString();
	}
}